using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FluentSilverlight.Expressions.Font;

namespace FluentSilverlight.Expressions
{
    public abstract class ControlExpression<THIS, TModel, VIEW> : FrameworkElementExpression<THIS, TModel, VIEW>
        where THIS : ControlExpression<THIS, TModel, VIEW>
        where TModel : class
        where VIEW : Control
    {
        private SolidColorBrush background;
        private SolidColorBrush foreground;
        private double fontSize = double.NaN;
        private FontWeight fontWeight = FontWeights.Normal;
        private FontStyle fontStyle = FontStyles.Normal;
        private Thickness padding;
        private bool isEnabled = true;
        private bool isTabStop = true;
        private string template;

        protected ControlExpression()
        {
        }

        protected ControlExpression(string name) : base(name)
        {
        }

        public THIS Background(Color color)
        {
            background = new SolidColorBrush(color);
            return thisInstance();
        }

        public THIS IsTabStop(bool value)
        {
            isTabStop = value;
            return thisInstance();
        }

        public THIS Enable()
        {
            isEnabled = true;
            return thisInstance();
        }

        public THIS Disable()
        {
            isEnabled = false;
            return thisInstance();
        }

        public THIS Foreground(Color color)
        {
            foreground = new SolidColorBrush(color);
            return thisInstance();
        }

        public THIS Padding(double uniformWidth)
        {
            padding = new Thickness(uniformWidth);
            return thisInstance();
        }

        public THIS Padding(double left, double top, double right, double bottom)
        {
            padding = new Thickness(left, top, right, bottom);
            return thisInstance();
        }

        public THIS FontSize(double size)
        {
            fontSize = size;
            return thisInstance();
        }

        public FontWeightExpression<THIS> FontWeight
        {
            get { return new FontWeightExpression<THIS>(thisInstance(), fw => fontWeight = fw); }
        }

        public FontStyleExpression<THIS> FontStyle
        {
            get { return new FontStyleExpression<THIS>(thisInstance(), fs => fontStyle = fs); }
        }

        public THIS Template(string templateName)
        {
            template = templateName;
            return thisInstance();
        }

        protected override void BuildUp(VIEW control)
        {
            base.BuildUp(control);

            control.IsEnabled = isEnabled;
            control.IsTabStop = isTabStop;
            control.Padding = padding;
            if (foreground != null) control.Foreground = foreground;
            if (background != null) control.Background = background;
            if (!double.IsNaN(fontSize)) control.FontSize = fontSize;
            control.FontWeight = fontWeight;
            control.FontStyle = fontStyle;
            if (template != null) control.Template = (ControlTemplate)Application.Current.Resources[template];
        }
    }
}